﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;

using Game.Editor.Toolbox;
using Game.Map;
using Game.Map.Extensions;
using Game.Objects;
using Game.Surface;
using Game.Toolkit;
using Game.Types;

namespace Game.Editor
{
    public class EditorSurfaceControl2 : SurfaceControl
    {
        public EditorSurfaceControl2()
            : this(Factory.Create())
        {
        }

        public EditorSurfaceControl2(IGameMap map)
            : base(map)
        {
            Click += delegate(object sender, GameMapEventArgs e)
            {
                if (SelectedTerrainType.TerrainType != null)
                {
                    e.MapField.TerrianX().TerrainType = SelectedTerrainType.TerrainType;
                }

                if (SelectedMapItemType.MapItemType != null)
                {
                    var obj = new GameObject(
                        SelectedMapItemType.MapItemType as ObjectType);
                    e.MapField.ObjectX().GameObject = obj;
                    SelectedMapItemType.MapItemType = null;
                }

                if (SelectedVehicleType.VehicleType != null)
                {
                    var v = new Vehicle(SelectedVehicleType.VehicleType);
                    e.MapField.VehicleX().Vehicle = v;
                    SelectedVehicleType.VehicleType = null;
                }

                if (SelectedInfrastructure.ObjectType != null)
                {
                    var v = new GameObject(SelectedInfrastructure.ObjectType);
                    e.MapField.ObjectX().GameObject = v;
                    SelectedInfrastructure.ObjectType = null;
                }

                if (SelectedBuilding.GameObjectGroup != null)
                {
                    GameObjectGroup t = SelectedBuilding.GameObjectGroup;
                    SelectedTerrainType.TerrainType = null;
                    SelectedGameObjectGroup.GameObjectGroup = null;
                    SelectedBuilding.GameObjectGroup = null;

                    List<GameObjectGroupPart> parts = t.Parts;

                    Point center = e.MapField.Point;
                    var building = new GameBuilding(t.BuildingType);
                    GameMapView.GameMap.GlobalItems.Add(building);

                    foreach (GameObjectGroupPart part in parts)
                    {
                        AssignObject(
                            (part.GetPoint(center).X),
                            part.ObjectType,
                            part.GameDirection,
                            e, building);
                    }
                }
            };
        }

        void AssignObject(
            double x,
            ObjectType item,
            GameDirection direction,
            GameMapEventArgs e,
            GameBuilding building = null)
        {
            double dx = e.MapField.Point.X;
            double dy = e.MapField.Point.Y;

            dx += direction.Value.X;
            dy += direction.Value.Y;

            int offset = ((e.MapField.Point.X % 2) == 0) ? 1 : 0;
            if ((x % 2) == 1)
            {
                dy += offset;
            }

            var origin = new Point(dx, dy);
            var field = MapUtil.GetField(GameMapView.GameMap,
                origin, Common.ObjectOrientation.Center);

            if (field == null)
            {
                Debugger.Break();
            }
            else
            {
                if (building != null)
                {
                    field.ObjectX().GameObject = new GameObject(item, building);
                }
                else
                {
                    field.ObjectX().GameObject = new GameObject(item);
                }
            }
        }

        protected AbstractField CreateSurfaceField(
            IMapFieldView field,
            AbstractField top,
            AbstractField cloud)
        {
            return new EditorSurfaceField(field, top, cloud);
        }

    }
}
